package com.bo.baby2.service.impl;

import com.bo.baby2.config.ReturnResult;
import com.bo.baby2.dao.*;
import com.bo.baby2.entity.*;
import com.bo.baby2.service.BankCardService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.UUID;

/**
 * @program: baby2_p2p
 * @description:
 * @author:波
 * @create: 2020-07-14 21:33
 **/
@Service
public class BankCardServiceImpl implements BankCardService {
    @Resource
    private TBankCardMapper tBankCardMapper;

    @Resource
    private TRechargeMapper tRechargeMapper;
    @Resource
    private TUserAccountMapper tUserAccountMapper;
    @Resource
    private TUserWalletMapper tUserWalletMapper;

    @Resource
    private TAccountFlowMapper tAccountFlowMapper;
    @Override
    public TBankCard selectByUserId(String userId) {
        return tBankCardMapper.selectByUserId(userId);
    }

    @Override
    public ReturnResult insertSelective(TBankCard record) {
        //查询是否是首次绑卡，首次绑卡说明没值
        TBankCard tBankCard = tBankCardMapper.selectByUserId(record.getUserId());
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        record.setId(uuid);
        if(tBankCard==null){
            record.setBalance(10000*100L);
        }
        int i = tBankCardMapper.insertSelective(record);
        if(i>0){
            return ReturnResult.successResult("绑定成功",null);
        }else {
            return ReturnResult.failResult("绑定失败");
        }

    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    /**
     * 充值方法 银行卡充到平台
     */
    public ReturnResult updateBalance(TRecharge recharge) {
        // TODO: 2020-07-16  
        ReturnResult returnResult=null;
        //根据银行卡Id查询余额
        TBankCard balanceByCardNumber = tBankCardMapper.getBalanceByCardNumber(recharge.getBankCardId());
        if(recharge.getAmount()<=0){
            return ReturnResult.failResult("充值失败，充值额度不能为0");
        }else if(recharge.getAmount()>balanceByCardNumber.getBalance()){
            return ReturnResult.failResult("充值失败，银行卡余额不足");
        }
        TBankCard tBankCard=new TBankCard();
        tBankCard.setId(recharge.getBankCardId());//银行卡Id
        tBankCard.setBalance(recharge.getAmount()); //设置扣除金额
        //修改金额
        int i = tBankCardMapper.updateBalanceT(tBankCard);
        if(i>0){
            String uuid=UUID.randomUUID().toString().replace("-","");
            recharge.setId(uuid);
            recharge.setRechargeTime(LocalDateTime.now());
            String jyh=String.valueOf(System.currentTimeMillis())+Math.round(Math.random()+1*1000);
            recharge.setTradeNo(jyh);

            //修改成功之后新增充值记录表
            int i2 = tRechargeMapper.insertSelective(recharge);
            if(i2>0){
                returnResult=ReturnResult.successResult("充值提交成功",null);
            }else {
                returnResult=ReturnResult.failResult("充值提交失败");
            }
        }

        return returnResult;
    }

    /**
     * 充值审核方法
     * @return
     */
    public ReturnResult rechargeAudit(String    id,Integer state){
        //查询出充值记录表的信息
        TRecharge tRecharge = tRechargeMapper.selectByPrimaryKey(id);
        //查询出用户资金信息
        TUserWallet tUserWallet = tUserWalletMapper.selectByPrimaryKey(tRecharge.getUserId());
        //审核拒绝的情况
        //审核拒绝后需要把银行卡充值进来钱的退回，然后在修改审核状态
        if(state==0){
                TBankCard tBankCard=new TBankCard();
                tBankCard.setBalance(tRecharge.getAmount());//设置退回的金额
                tBankCard.setUserId(tRecharge.getBankCardId());//设置退回的银行卡Id
            //把钱退回到银行卡
            int i = tBankCardMapper.updateBalance(tBankCard);
            TRecharge tRecharge1=new TRecharge();
            tRecharge1.setId(tRecharge.getId());
            tRecharge1.setState(0);
            //修改审核状态
            int i1 = tRechargeMapper.updateByPrimaryKeySelective(tRecharge1);
            if(i1>0&&i>0){
                //把充值失败的记录存到流水表里
                TAccountFlow tAccountFlow=new TAccountFlow();
                tAccountFlow.setAmount(tRecharge.getAmount());
                tAccountFlow.setAvailableAmount(tUserWallet.getAvailableAmount());
                tAccountFlow.setFreezeAmount(tUserWallet.getFreezeAmount());
                tAccountFlow.setId(System.currentTimeMillis());
                tAccountFlow.setFlowType(50);
                tAccountFlow.setRemark("充值失败,充值金额："+tRecharge.getAmount()*0.01);
                tAccountFlow.setAccountId(tUserWallet.getAccountId());
                tAccountFlowMapper.insertSelective(tAccountFlow);
            }
            //审核通过的情况
         //审核通过后需要把钱充值到账户里，然后在修改审核状态
        }else if(state==2){
            TUserWallet tUserWallet1=new TUserWallet();
            tUserWallet1.setAvailableAmount(tRecharge.getAmount());    //设置增加的钱
            tUserWallet1.setAccountId(tRecharge.getUserId());
            //修改金额
            int i = tUserWalletMapper.updateAmountJ(tUserWallet1);
            //修改状态
            TRecharge tRecharge1=new TRecharge();
            tRecharge1.setId(tRecharge.getId());
            tRecharge1.setState(2);
            //修改审核状态
            int i1 = tRechargeMapper.updateByPrimaryKeySelective(tRecharge1);
            if(i1>0&&i>0){
                //查询出充值成功后用户资金信息
                TUserWallet tUserWallet2 = tUserWalletMapper.selectByPrimaryKey(tRecharge.getUserId());
                //把充值成功写入流水表
                TAccountFlow tAccountFlow=new TAccountFlow();
                tAccountFlow.setAmount(tRecharge.getAmount());
                tAccountFlow.setAvailableAmount(tUserWallet2.getAvailableAmount());
                tAccountFlow.setFreezeAmount(tUserWallet2.getFreezeAmount());
                tAccountFlow.setId(System.currentTimeMillis());
                tAccountFlow.setFlowType(50);
                tAccountFlow.setRemark("充值成功，充值金额为："+tRecharge.getAmount()*0.01);
                tAccountFlow.setAccountId(tUserWallet2.getAccountId());
                tAccountFlowMapper.insertSelective(tAccountFlow);
            }
        }
        return ReturnResult.successResult("成功",null);
    }
}
